using System;
using System.Data;
using System.Collections.Generic;
using Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter;
using System.IO;

public partial class CreateUser : System.Web.UI.Page
{
    Practice _providerPractice;
    List<Practice> _practiceCollection = new List<Practice>();
    string _selectedUser = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        AddJavaScript();
        if (!IsPostBack)
        {
            _providerPractice = new Practice(Path.Combine(Server.MapPath(@"Data"), Constants.PracticeXmlFileName));
            DataTable practiceNameCollection = new DataTable();
            _providerPractice = new Practice(Path.Combine(Server.MapPath(@"Data"), Constants.PracticeXmlFileName));
            _practiceCollection = _providerPractice.SelectPracticeRecords("");
            practiceNameCollection.Columns.Add("PracticesID");
            practiceNameCollection.Columns.Add("PracticesName");

            foreach (Practice practice in _practiceCollection)
            {
                object[] parameters = new object[2];
                parameters[0] = practice.PracticeId.ToString();
                parameters[1] = practice.PracticeName;
                practiceNameCollection.Rows.Add(parameters);
            }

            DropDownListProviderPractice.DataSource = practiceNameCollection;
            DropDownListProviderPractice.DataTextField = "PracticesName";
            DropDownListProviderPractice.DataValueField = "PracticesID";
            DropDownListProviderPractice.DataBind();
        }
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        Users users = new Users(Path.Combine(Server.MapPath(@"Data"), Constants.PracticeXmlFileName));
        users.UserId = Guid.NewGuid();
        users.UserName = TextBoxUsername.Text.Trim();
        users.Password = TextBoxPassword.Text.Trim();
        users.PracticeId = new Guid(DropDownListProviderPractice.SelectedItem.Value);
        users.AddUser();
        Response.Redirect("Login.aspx", false);
    }
    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx", false);
    }

    /// <summary>
    /// Add Java Scripts in Current page controls. 
    /// </summary>   
    private void AddJavaScript()
    {
        TextBoxUsername.Focus();
        TextBoxUsername.Attributes.Add("Onkeypress", "CheckSpecialCharacter('<,>,&,\",(,),^,!,#,$,%,*,+,=,;,:,{,}')");
        TextBoxPassword.Attributes.Add("Onkeypress", "CheckSpecialCharacter('<,>,\",(,),%,+,=,;,:,{,}')");
        TextBoxConfirmPassword.Attributes.Add("Onkeypress", "CheckSpecialCharacter('<,>,\",(,),%,+,=,;,:,{,}')");
    }
}
